package com.zrrd.bufferedStream.bufferCopy;

import java.io.*;

/**
 * @author WanJl
 * @version 1.0
 * @title BufferedCopy
 * @description
 * @create 2024/5/25
 */
public class BufferedCopy01 {

    /**
     * 复制
     * @param starPath 起始文件路径
     * @param destPath 目标文件路径
     */
    public static void copy(String starPath, String destPath) {
        //输入
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        //输出
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            //创建字节输入流对象，以文件路径为参数，操作的是文件
            fis = new FileInputStream(starPath);
            //创建字节缓冲输入流对象，以字节输入流对象为参数，操作的是字节输入流
            bis = new BufferedInputStream(fis);

            fos = new FileOutputStream(destPath);
            bos = new BufferedOutputStream(fos);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, length);
            }

        }catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                //释放资源
                bis.close();//3
                fis.close();//4
                bos.close();//1
                fos.close();//2
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        copy("E:/MavenRepository.zip","D:/MavenRepository.zip");
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
